using System;
using System.Configuration;
using System.Web.UI;

using ScenarioFramework.Modules.DM.DataBase;
using ScenarioFramework.Modules.DM.DataModel;
using System.Web.UI.WebControls;

namespace ACEMPortal.UserControls
{
	/// <summary>
	///		Summary description for BPAVerticalBar.
	/// </summary>
	public partial class DocumentTree : System.Web.UI.UserControl
	{
		//protected OboutInc.SlideMenu.SlideMenu BPADocumentBrowser;

		protected DocumentManagerDBHelper dmDBHelper = null;
		protected System.Web.UI.WebControls.PlaceHolder Documents;

		public bool DocumentInstance = false;

		public string IdProcess = string.Empty;
		public string IdActivity = string.Empty;

		public bool OnlyDocumentId = false;

		public string ActvityUrl = string.Empty;


		protected void Page_Load(object sender, System.EventArgs e)
		{
			TreeNode root = new TreeNode();

			IdProcess = Request.QueryString["IdProcess"];
			IdActivity = Request.QueryString["IdActivity"];

			dmDBHelper = new DocumentManagerDBHelper();
			XMLDocumentInstance document = null;
			if ( DocumentInstance )
			{
				document = (XMLDocumentInstance)Session["MasterDocumentInstance"];
			}
			else
			{
				document = dmDBHelper.GetDocumentInstance(ConfigurationSettings.AppSettings["Documento_Master"]);
			}

			XMLDocuments xmlDocuments = document.XMLDocumentVersions.GetLastVersion().XMLDocuments;
			root.Text = document.Id;
			string html  = string.Empty;
			if ( this.OnlyDocumentId )
			{
				html = "/SF/Modules/DM/DocumentInstanceInfo.aspx?DocumentId=" + document.Id;
			}
			else
			{
				string sASPXPage = "/SF/Modules/DM/";
				sASPXPage+=this.ActvityUrl;
				sASPXPage+=".aspx?IdProcess=" + this.IdProcess + "&IdActivity=" + this.IdActivity;
				html = sASPXPage + "&DocumentId=" + document.Id;
			}
			root.NavigateUrl  = html;


			for ( int i=0; i<xmlDocuments.Count;i++)
			{
				XMLDocumentInstance docInstance = (XMLDocumentInstance)xmlDocuments[i];
				string sDocInstanceId = docInstance.Id;

				this.AddDocumentToTree (ref root, "root", sDocInstanceId, "DocumentationRepository");
			}

			this.tvDocuments.Nodes.Add( root );

			
	
			//this.InizializeServices();
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
	
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{

		}
		#endregion

		private void AddDocumentToTree(ref TreeNode root, string sNode, string sDocumentId, string sTreeName)
		{
			XMLDocumentInstance document = dmDBHelper.GetDocumentInstance(sDocumentId);

			bool bRet = ScenarioFramework.WAPI.Role.CheckGrant( document.XMLGrants, Page.User.Identity.Name, "VISIBLE");
			if ( bRet )
			{
				string sASPXPage = "DocumentViewer.aspx";

				XMLDocuments xmlDocuments = document.XMLDocumentVersions.GetLastVersion().XMLDocuments;
				string sNodeName = document.Name.Replace (" ", "_");
				string html  = string.Empty;
				if ( this.OnlyDocumentId )
				{
					html = "/SF/Modules/DM/DocumentInstanceInfo.aspx?DocumentId=" + document.Id;
				}
				else
				{
                    sASPXPage = "/SF/Modules/DM/";
					sASPXPage+=this.ActvityUrl;
					sASPXPage+=".aspx?IdProcess=" + this.IdProcess + "&IdActivity=" + this.IdActivity;
					html = sASPXPage + "&DocumentId=" + document.Id;
				}
				
				
				
				TreeNode treeNodeAttribute = new TreeNode();
				treeNodeAttribute.Text = document.Id;
				treeNodeAttribute.NavigateUrl  = html;

				root.ChildNodes.Add (treeNodeAttribute);

				for ( int i=0; i<xmlDocuments.Count;i++)
				{
					XMLDocumentInstance docInstance = (XMLDocumentInstance)xmlDocuments[i];

					this.AddDocumentToTree (ref treeNodeAttribute, sNodeName, docInstance.Id, sTreeName);
				}
			}
		}
	}
}
